package sample.algo.sistr.algosample.algos.graphs;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * Created by sistr on 28/09/14.
 */
public class Node {
    public final int id;

    public enum State {
        NORMAL,
        HIGHLIGHTED,
    }

    public final LinkedList<Edge> out_edges;
    private final LinkedList<Edge> in_edges;

    public State state;

    // For path finding algorithm
    public float reaching_cost;
    public Node predecessor;

    // For graphic display
    public float x;
    public float y;

    /**
     * @param nodeId is final and is used to identify a node. Must be unique for each node created.
     * @param x used for display. Express in percentage of the the total rendering width.
     * @param y used for display. Express in percentage of the the total rendering height.
     */
    public Node(int nodeId, float x, float y) {
        id = nodeId;
        out_edges = new LinkedList<Edge>();
        in_edges = new LinkedList<Edge>();

        this.x = x;
        this.y = y;

        state = State.NORMAL;

        reaching_cost = -2;
        predecessor = null;
    }

    public void AddEdge(Node destination, float weight) {

        for(Edge e : out_edges) {
            if(e.end.id == destination.id) {
                e.weight = weight;
                e.end.AddInputEdge(this, weight);
                return;
            }
        }

        out_edges.add(new Edge(this, destination, weight));
        destination.AddInputEdge(this, weight);
    }

    private void AddInputEdge(Node source, float weight) {
        for(Edge e : in_edges) {
            if(e.start.id == source.id) {
                e.weight = weight;
                return;
            }
        }

        in_edges.add(new Edge(source, this, weight));
    }
}
